package cn.xfdzx.android.apps.shop.activity.myshop;

import android.app.Dialog;
import android.content.Intent;
import android.net.Uri;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import androidx.recyclerview.widget.DefaultItemAnimator;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;
import java.util.List;

import cn.xfdzx.android.apps.shop.R;
import cn.xfdzx.android.apps.shop.adapter.OrderDetailPringListAdapter;
import cn.xfdzx.android.apps.shop.apps.Constants;
import cn.xfdzx.android.apps.shop.base.BaseMvpActivity;
import cn.xfdzx.android.apps.shop.bean.ConfirmBeen;
import cn.xfdzx.android.apps.shop.bean.DetailPringBean;
import cn.xfdzx.android.apps.shop.contract.OrderDetailPringContract;
import cn.xfdzx.android.apps.shop.evenbus.UpdateCancelOrderMessage;
import cn.xfdzx.android.apps.shop.evenbus.UpdateShopOrderMessage;
import cn.xfdzx.android.apps.shop.presenter.OrderDetailPringPresenter;
import cn.xfdzx.android.apps.shop.util.EmptyUtil;
import cn.xfdzx.android.apps.shop.util.ProgressDialog;
import cn.xfdzx.android.apps.shop.util.StatusBarUtil;
import cn.xfdzx.android.apps.shop.util.Utils;

/*
/* 商家订单详情页面
 */
public class OrderDetailPringActivity extends BaseMvpActivity<OrderDetailPringPresenter> implements OrderDetailPringContract.View, View.OnClickListener {
    private ImageView ivBack;      //标题栏-左边按钮
    private RecyclerView recyclerView;
    private List<DetailPringBean.DataBean.GoodsListBean> goods_list = new ArrayList<>();
    private RelativeLayout.LayoutParams item_bar;
    private LinearLayout llState;
    private String orderId;
    private boolean is_ti;
    private OrderDetailPringListAdapter orderDetailPringListAdapter;

    private TextView tv_store_name;
    private TextView tv_ordersort;
    private TextView tv_orderstate;
    private TextView tv_order_num;
    private TextView tv_order_time;
    private TextView tv_allweight;
    private TextView tv_allnum;
    private TextView tv_address;
    private TextView tv_receive_mobile;
    private RelativeLayout rl_bottom;
    private TextView tv_cancleorder;//取消订单
    private TextView tv_customer_mobile;
    private TextView tv_totalmoney;
    private TextView tv_qishou_mobile;
    private Dialog dialog;
    private DetailPringBean.DataBean data;
    private TextView tvZiti;

    @Override
    public int getLayoutId() {
        return R.layout.activity_orderdetailpring;
    }

    @Override
    public void initView() {
        orderId = getIntent().getStringExtra("orderId");
        is_ti = getIntent().getBooleanExtra("is_ti",false);

        mPresenter = new OrderDetailPringPresenter();
        mPresenter.attachView(this);

        recyclerView = findViewById(R.id.recycle_orderlist);
        ivBack = findViewById(R.id.iv_back);
        llState = findViewById(R.id.ll_state);
        tvZiti = findViewById(R.id.tv_ziti);
        tv_store_name = findViewById(R.id.tv_store_name);
        tv_ordersort = findViewById(R.id.tv_ordersort);
        tv_orderstate = findViewById(R.id.tv_orderstate);
        tv_order_num = findViewById(R.id.tv_order_num);
        tv_order_time = findViewById(R.id.tv_order_time);
        tv_allweight = findViewById(R.id.tv_allweight);
        tv_allnum = findViewById(R.id.tv_allnum);
        tv_address = findViewById(R.id.tv_address);
        tv_receive_mobile = findViewById(R.id.tv_receive_mobile);
        rl_bottom = findViewById(R.id.rl_bottom);
        tv_cancleorder = findViewById(R.id.tv_cancleorder);
        tv_customer_mobile = findViewById(R.id.tv_customer_mobile);
        tv_totalmoney = findViewById(R.id.tv_totalmoney);
        tv_qishou_mobile = findViewById(R.id.tv_qishou_mobile);

        ivBack.setOnClickListener(this);
        tv_cancleorder.setOnClickListener(this);
        tv_receive_mobile.setOnClickListener(this);
        tv_qishou_mobile.setOnClickListener(this);
        tv_customer_mobile.setOnClickListener(this);
        tv_order_num.setOnClickListener(this);
        tvZiti.setOnClickListener(this);
        initHigh();
        initRecycle();
        mPresenter.getOrderDetailPring(orderId);

        dialog = Utils.addDialog(this, R.layout.dialog, R.string.confirmcancleorder, R.string.confirm, new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                mPresenter.getCancleOrder(orderId);
                dialog.dismiss();
            }
        });

        if (is_ti){
            tvZiti.setVisibility(View.VISIBLE);
            tvZiti.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    mPresenter.confirm(orderId,0);
                }
            });
        }else {
            tvZiti.setVisibility(View.GONE);
        }

    }


    private void initHigh() {

        item_bar = (RelativeLayout.LayoutParams) llState.getLayoutParams();
        item_bar.height = StatusBarUtil.getStatusBarHeight(this);
        llState.setLayoutParams(item_bar);
    }

    private void initRecycle() {
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        recyclerView.setItemAnimator(new DefaultItemAnimator());
        orderDetailPringListAdapter = new OrderDetailPringListAdapter(goods_list);
        recyclerView.setAdapter(orderDetailPringListAdapter);

     }

    @Override
    protected void onResume() {
        super.onResume();
//        page = 1;
//        mPresenter.getFollowList(true,true,limit,page);
    }

    @Override
    public void finishRefresh() {

    }

    @Override
    public void finishLoadMore() {

    }

    @Override
    protected boolean isUseFullScreenMode() {
        return true;
    }

    @Override
    public void showLoading() {
        if (!ProgressDialog.getInstance().materialDialog.isShowing()) {
            ProgressDialog.getInstance().show(this);
        }
    }

    @Override
    public void hideLoading() {
        ProgressDialog.getInstance().dismiss();
    }

    @Override
    public void onError(Throwable throwable) {

    }

    @Override
    public void getOrderDetailPringSuccess(DetailPringBean bean) {
        if (bean.getStatus() == Constants.SUCCESS_CODE) {
            data = bean.getData();
            goods_list = bean.getData().getGoods_list();
             orderDetailPringListAdapter.refreshDatas(goods_list);

            tv_store_name.setText("店铺名称: "+bean.getData().getStore_name());
            tv_ordersort.setText("订单序列: 第"+bean.getData().getOrder_day_number()+"单");
            tv_order_num.setText("订单号: "+orderId);
            tv_order_time.setText("下单时间: "+bean.getData().getCurrent_order_time());
            tv_allweight.setText("总重量: "+bean.getData().getGoods_total_weight()+bean.getData().getUnit());
            tv_allnum.setText("总数量: "+bean.getData().getGoods_total_num()+"件");
            tv_address.setText("收货人地址: "+bean.getData().getReceive_address());
            tv_receive_mobile.setText("收货人电话: "+bean.getData().getUser_phone());
            tv_customer_mobile.setText("客服电话: "+bean.getData().getPlatform_phone());
            tv_totalmoney.setText("总费用: "+bean.getData().getGoods_total_price()+"元");
            if (EmptyUtil.isNotEmpty(bean.getData().getOperator_phone())){
                tv_qishou_mobile.setVisibility(View.VISIBLE);
                tv_qishou_mobile.setText("骑手电话: "+bean.getData().getOperator_phone());
            }else {
                tv_qishou_mobile.setVisibility(View.GONE);
            }

            //'0 待发货 1 已发货 2 已收货待评价 3 已评价 4已完成 5已关闭 6申请退款 7取消订单'
            switch (bean.getData().getStatus()){
                case "-1":
                    rl_bottom.setVisibility(View.GONE);
                    tv_orderstate.setText("订单状态: 未支付");
                    break;
                case "0":
                    rl_bottom.setVisibility(View.VISIBLE);
                    tv_orderstate.setText("订单状态: 待发货");
                    break;
                case "1":
                    rl_bottom.setVisibility(View.VISIBLE);
                    tv_orderstate.setText("订单状态: 已发货");
                    break;
                case "2":
                    rl_bottom.setVisibility(View.GONE);
                    tv_orderstate.setText("订单状态: 已收货");
                    break;
                case "3":
                    rl_bottom.setVisibility(View.GONE);
                    tv_orderstate.setText("订单状态: 已评价");
                    break;
                case "4":
                    rl_bottom.setVisibility(View.GONE);
                    tv_orderstate.setText("订单状态: 已完成");
                    break;
                case "5":
                    rl_bottom.setVisibility(View.GONE);
                    tv_orderstate.setText("订单状态: 已关闭");
                    break;
                case "6":
                    rl_bottom.setVisibility(View.GONE);
                    tv_orderstate.setText("订单状态: 已售后");
                    break;
                case "7":
                    rl_bottom.setVisibility(View.GONE);
                    tv_orderstate.setText("订单状态: 已取消");
                    break;
            }
        }else {
            Toast.makeText(this, bean.getMsg(), Toast.LENGTH_SHORT).show();
        }
    }

    @Override
    public void getCancleOrderSuccess(DetailPringBean bean) {
        if (bean.getStatus() == Constants.SUCCESS_CODE) {
            Toast.makeText(this, "订单已取消", Toast.LENGTH_SHORT).show();
            EventBus.getDefault().post(new UpdateCancelOrderMessage(""));
            finish();
        }else {
            Toast.makeText(this, bean.getMsg(), Toast.LENGTH_SHORT).show();
        }

    }

    @Override
    public void onConfirmSuccess(ConfirmBeen bean, int position) {
        if (bean.getStatus() == Constants.SUCCESS_CODE) {
            rl_bottom.setVisibility(View.GONE);
            EventBus.getDefault().post(new UpdateShopOrderMessage("1"));
            EventBus.getDefault().post(new UpdateShopOrderMessage("2"));
        }  else {
            Toast.makeText(this, bean.getMsg(), Toast.LENGTH_SHORT).show();
        }
    }

    @Override
    protected void onDestroy() {
        if (EmptyUtil.isNotEmpty(mPresenter))
        mPresenter.detachView();
        super.onDestroy();
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.tv_ziti:

                break;
            case R.id.iv_back:    //返回按钮点击
                finish();
                break;
            case R.id.tv_cancleorder:    //取消订单
                dialog.show();
                break;
            case R.id.tv_receive_mobile:    //
                if (EmptyUtil.isNotEmpty(data.getUser_phone())){
                    callPhone(data.getUser_phone());
                }
                break;
            case R.id.tv_qishou_mobile:    //
                if (EmptyUtil.isNotEmpty(data.getOperator_phone())){
                    callPhone(data.getOperator_phone());
                }
                break;
            case R.id.tv_customer_mobile:    //
                if (EmptyUtil.isNotEmpty(data.getPlatform_phone())){
                    callPhone(data.getPlatform_phone());
                }
                break;
            case R.id.tv_order_num:    //
                Utils.copy(this, orderId);
                Toast.makeText(this, "复制成功", Toast.LENGTH_SHORT).show();
                break;


        }
    }

    /**
     * 拨打电话（跳转到拨号界面，用户手动点击拨打）
     *
     * @param phoneNum 电话号码
     */
    public void callPhone(String phoneNum) {
        Intent intent = new Intent(Intent.ACTION_DIAL);
        Uri data = Uri.parse("tel:" + phoneNum);
        intent.setData(data);
        startActivity(intent);
    }
}
